﻿CREATE PROCEDURE [dbo].[spGetInvoiceLinesFromStaging] 
	@LastETLTime datetime
AS
BEGIN
    IF (@LastETLTime is null)
		set @LastETLTime = '1900-01-01'

	SELECT I.[InvoiceCode]
      ,[CustomerCode]
      ,[InvoiceDate]
      ,[BillingAddress]
      ,[BillingCity]
      ,[BillingState]
      ,[BillingCountry]
      ,[BillingPostalCode]
      ,[Total]
	  ,[InvoiceLineCode]
      ,IL.[TrackCode]
      ,IL.[UnitPrice]
      ,[Quantity]
	  ,T.MediaTypeCode
	  ,T.GenreCode
	FROM [stg].[Invoice] I
		INNER JOIN [stg].[InvoiceLine] IL ON I.[InvoiceCode] = IL.[InvoiceCode]
		LEFT JOIN [stg].[Track] T ON T.TrackCode = IL.TrackCode
	WHERE [InvoiceDate]>@LastETLTime
END